<script setup lang="js">
import { MYURL as URL } from '@/utils/express/express.js'
import Vrouter from "@/router"
import { onMounted, ref, inject, onBeforeUnmount } from "vue"
import { get_other_info, togetherIsFan, get_user_info_with_token } from '@/api/user/user.js'
import { videosubscribe } from '@/api/video/video.js'
import isToken from '@/utils/token/token.js'
import { userStore } from "@/stores/user.js"
const FriendState = userStore()
let imgURL = `${URL}/public/icon/back_white.png`
import get_LV from '@/api/experience/experience.js'
const router = Vrouter
let lv_img = ref(`${URL}/public/icon/lv1.png`)
const getback = () => {
    router.go(-1)
}
const top_name = ref('none')
const get_back_main = ref(null)
const back_image = ref(null)
const name_active = ref(false)
const sendMessage = inject('change_message_state')
onMounted(() => {
    addEventListener('scroll', scrollStyle)
})
const scrollStyle = () => {
    if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
        get_back_main.value.style.backgroundColor = `white`
        top_name.value = 'flex'
        name_active.value = true
        back_image.value.style.backgroundColor = `rgba(0, 0, 0, 0)`
        imgURL = `${URL}/public/icon/back_black.png`
    } else {
        get_back_main.value.style.backgroundColor = `rgba(0, 0, 0, 0)`
        top_name.value = 'none'
        name_active.value = false
        back_image.value.style.backgroundColor = `rgba(0, 0, 0, 0.23)`
        imgURL = `${URL}/public/icon/back_white.png`
    }
}
onBeforeUnmount(() => {
    removeEventListener('scroll', scrollStyle)
})

const manORgril = ref('man')
const manORgril_URL = ref(`${URL}/public/icon/${manORgril.value}.png`)
const detail_click = ref(false)
const detail_text = ref('详情')
const detail_btn = () => {
    if (detail_click.value) {
        detail_text.value = '详情'
        detail_click.value = false
    } else {
        detail_text.value = '收起'
        detail_click.value = true
    }
}
const exp_length = ref('0')
const person_imformation = ref({})
const query = router.currentRoute.value.query.up
const isEmpty = ref(false)
FriendState.getOtherVideos(query)
onMounted(() => {
    const info_init = () => {
        if (isToken()) {
            return get_user_info_with_token(query)
        } else {
            return get_other_info(query)
        }
    }

    info_init().then((res) => {
        if (res.data.status === 0) {
            person_imformation.value.self = res.data.mes[0].self
            manORgril_URL.value = `${URL}/public/icon/${res.data.mes[0].sex}.png`
            person_imformation.value.sex = res.data.mes[0].sex
            res.data.mes[0].vip === '大会员' ? person_imformation.value.vip = true : person_imformation.value.vip = false
            person_imformation.value.username = res.data.mes[0].username
            person_imformation.value.good = res.data.mes[0].good
            person_imformation.value.about = res.data.mes[0].about
            res.data.mes[0].active === null ? person_imformation.value.active = 0 : person_imformation.value.active = res.data.mes[0].active
            res.data.mes[0].fans === null ? person_imformation.value.fans = 0 : person_imformation.value.fans = res.data.mes[0].fans.split(',').length
            res.data.mes[0].idol === null ? person_imformation.value.idol = 0 : person_imformation.value.idol = res.data.mes[0].idol.split(',').length
            person_imformation.value.profile = `${URL}/public/profiles/${res.data.mes[0].profile}`
            person_imformation.value.experience = res.data.mes[0].experience
            person_imformation.value.id = res.data.mes[0].id
            lv_img.value = `${URL}/public/icon/lv${get_LV(person_imformation.value.experience).LV}.png`
            person_imformation.value.experience_need = get_LV(person_imformation.value.experience).need
            isNaN(get_LV(person_imformation.value.experience).need) ? exp_length.value = 1 : exp_length.value = person_imformation.value.experience / get_LV(person_imformation.value.experience).need
            if (isToken()) {
                let data = {
                    account: query
                }
                togetherIsFan(data).then((res) => {
                    isFan.value = res.data.message
                })
            }
        } else {
            isEmpty.value = true
        }
    }).catch((err) => {
        console.log(err);
        isEmpty.value = true
    })
})
const goFan = (e) => {
    FriendState.changeFriendState(e)
    router.push(`/otherfriend?up=${query}`)
}
const isFan = ref(false)
const subscribe = () => {
    if (!isToken()) return router.push('/login')
    let data = {
        account: query
    }
    videosubscribe(data).then((res) => {
        if (res.data.status != 1) {
            isFan.value = !isFan.value
        }
        sendMessage(res.data.message)
    })
}
</script>
<template>
    <div v-if="isEmpty">
        <IsEmpty>查不到这个人哦</IsEmpty>
    </div>
    <div v-if="!isEmpty" style="background-color: white;">
        <div class="get-back-main" ref="get_back_main">
            <img class="back-image" ref="back_image" :src="imgURL" @click="getback">
            <div :style="{ display: top_name }" class="title">
                <p>{{ person_imformation.username }}</p>
            </div>
            <div v-show="!person_imformation.self" :class="{ letter_active: name_active }"
                @click="router.push(`/chat?id=${person_imformation.id}`)" class="letter">
                <img src="@/assets/img/chat.png">
            </div>
        </div>
        <!-- <div class="getback-placehoder"></div> -->
        <div class="user-top-img">
            <img src="@/assets/img/personback.png">
        </div>
        <div class="user-imformation">
            <div><img :src="person_imformation.profile"></div>
            <div>
                <div class="person_num">
                    <div @click="goFan(false)">
                        <p>{{ person_imformation.fans }}</p>
                        <p>粉丝</p>
                    </div>
                    <p>|</p>
                    <div @click="goFan(true)">
                        <p>{{ person_imformation.idol }}</p>
                        <p>关注</p>
                    </div>
                    <p>|</p>
                    <div>
                        <p>{{ person_imformation.good }}</p>
                        <p>获赞</p>
                    </div>
                </div>
                <div class="change-imformation">
                    <p @click="subscribe" v-show="!isFan"><b style="font-size: 15px;">+</b> 关注</p>
                    <div @click="subscribe" class="fan" v-show="isFan">
                        <img src="@/assets/img/table.png">
                        <p>已关注</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="user-name">
            <div class="user-name-top">
                <p>{{ person_imformation.username }}</p>
                <img :src="lv_img">
            </div>
        </div>
        <div class="user-about">
            <p :class="{ about_hideen: !detail_click }">{{ person_imformation.about }}</p>
            <span @click="detail_btn">{{ detail_text }}</span>
            <div v-show="detail_click">
                <!-- <img :src="manORgril_URL"> -->
                <img v-show="person_imformation.sex === 'man' || person_imformation.sex === 'gril'" :src="manORgril_URL">
                <p v-show="person_imformation.sex != 'man' && person_imformation.sex != 'gril'" class="sex_label">{{
                    person_imformation.sex === 'secret' ? '保密' : person_imformation.sex }}</p>

                <div>
                    <img src="@/assets/img/uid.png" alt="">
                    <p>{{ person_imformation.id }}</p>
                </div>
            </div>
        </div>
        <div class="title_nav">
            <router-link replace :to="'personal_detail' + '?up=' + query">主页</router-link>
            <router-link replace :to="'personal_add' + '?up=' + query">投稿</router-link>
            <router-link replace :to="'personal_star' + '?up=' + query">收藏</router-link>
            <router-link replace :to="'personal_good' + '?up=' + query">点赞</router-link>
        </div>
        <RouterView />
    </div>
</template>
<style scoped lang="less">
.sex_label {
    color: #979ba1;
    font-size: 9px;
    padding: 0 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 14px;
    border: .6px solid #979ba1;
}

.big_vip {
    background-color: var(--bilibili-pink) !important;
}

.get-back-main {
    background-color: rgba(0, 0, 0, 0);
    z-index: 100;

    p {
        color: #605f64;
    }

    .back-image {
        width: 30px;
        padding: 5px;
        background-color: rgba(0, 0, 0, 0.23);
        border-radius: 50%;
    }
}

.user-top-img {
    position: relative;

    img {
        width: 100vw;
    }
}

.letter {
    z-index: 100;
    position: absolute;
    right: 2%;
    top: 10px;
    background-color: rgba(0, 0, 0, 0.33);
    width: 30px;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 50%;

    img {
        width: 20px;
    }
}

.letter_active {
    background-color: white;

    img {
        filter: brightness(0.3);
    }

}

.user-imformation {
    padding: 0 10px 0 20px;
    display: flex;

    >div:last-child {
        width: 60%;
        margin-left: auto;
    }

    >div {
        display: flex;
        flex-direction: column;

        .change-imformation {
            >p {
                text-align: center;
                width: 100%;
                color: #f68db5;
                padding: 6px 0;
                border: #f68db5 1px solid;
                border-radius: 5px;
            }

            .fan {
                width: 100%;
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
                background-color: #e3e5e7;
                color: #95999f;
                padding: 8px 10px;
                border-radius: 5px;
                gap: 3px;

                p {

                    font-size: 13px;
                    font-weight: 500;
                    margin: 0;
                }

                img {
                    top: 0;
                    left: 0;
                    width: 20px;
                    height: 20px;
                    border: none;
                }
            }


        }

        img {
            position: relative;
            top: -30px;
            height: 90px;
            width: 90px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px white solid;
        }

        >div {
            display: flex;
            justify-content: space-between;
            align-items: center;

            >p {
                color: #9e9da2;
            }

            >div {
                display: flex;
                flex-direction: column;
                align-items: center;

                p:last-child {
                    font-size: 12px;
                    color: #979ba1;
                }
            }
        }

        p {
            margin: auto;
        }
    }
}

.user-name {
    display: inline-flex;
    flex-direction: column;
    padding: 0 10px;

    .user-name-top {
        display: flex;
        gap: 5px;
        align-items: center;

        >p:nth-child(1) {
            font-size: 16px;
            font-weight: 600;
        }

        img {
            width: 20px;
        }
    }

    .user-experience {
        .user-exp-top {
            display: flex;
            align-items: center;
            gap: 8px;

            img {
                width: 20px;
            }

            p {
                color: #95999f;
                font-size: 10px;
                font-weight: 600;
            }
        }

        .user-exp-bottom {
            width: 100%;
            height: 3px;
            border-radius: 10px;
            background-color: #95999f;
            overflow: hidden;

            >div {
                height: 100%;
                background-color: #d57545;
            }
        }
    }
}

.user-about {
    width: 100%;
    margin-top: 5px;
    padding: 0 10px 10px 10px;
    position: relative;

    >p {
        width: 90%;
        font-size: 12px;
        color: #95999f;
        overflow: auto;
        text-overflow: ellipsis;
        white-space: wrap;
    }

    >span {
        position: absolute;
        right: 0;
        top: 0;
        transform: translate(-50%, 0);
        font-size: 13px;
        color: #3c88c0;
    }

    >div {
        margin-top: 0px;
        display: flex;
        align-items: center;

        >img {
            width: 10px;
        }

        >div {
            display: flex;
            align-items: center;

            img {
                width: 20px;
            }

            p {
                font-size: 10px;
                color: #95999f;
            }
        }

    }
}

.about_hideen {
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
}

.title_nav {
    position: sticky;
    top: 40px;
    background-color: white;
    padding: 0 0 10px 0;
    display: flex;
    justify-content: space-around;
    margin-top: 10px;
    border-top: 0.1px rgba(0, 0, 0, 0.13) solid;
    padding-top: 10px;

    a {
        font-size: 14px;
        color: #605f64;
    }
}

.router-link-active {
    color: #f68db5 !important;
}
</style>